package view.tablecellrenderer;

import java.awt.Color;
import java.awt.Component;

import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;

import model.os.OS;
import model.os.OSProcess;
import model.os.PageTable;
import model.os.PageTableEntry;
import model.system.HardDisk.Slot;

import view.HardDiskView;

public class HardDiskViewCellRenderer extends DefaultTableCellRenderer  {

	private static final long serialVersionUID = 6094492822926395670L;

	@Override
	public Component getTableCellRendererComponent(JTable table, Object value,
			boolean isSelected, boolean hasFocus, int row, int column) {

		Component component = super.getTableCellRendererComponent(table, value,
				isSelected, hasFocus, row, column);
		
		int slotNumber = row * HardDiskView.COLUMNS + column;
		
		Slot slot = OS.getInstance().getHardDisk().getSlot(slotNumber);
		OSProcess process = slot.getProcess();
		
		Color color = null;
		
		if (process != null) {
			PageTable pageTable = OS.getInstance().getPageTable(process);
			PageTableEntry tableEntry = pageTable.getPageTableEntry(slot.getPage().getNumber());
			
			if (!tableEntry.isValid()) {
				color = process.getColor();
			} else {
				color = Color.GREEN;
			}
		}
		
		component.setBackground(color);
		
		return component;
	}
}
